#include <stdio.h>
#include <stdlib.h>
#include "fm_records.h"

fm_records_t fm_records;

#if 0
void fm_records_test()
{
    fm_records_init();

    int ret=0;
    ret=fm_records_set("path1");
    if(ret<0)
    {
        dprintf("set error\n");
        return ;
    }
    ret=fm_records_set("path2");
    if(ret<0)
    {
        dprintf("set error\n");
        return ;
    }
    ret=fm_records_set("path3");
    if(ret<0)
    {
        dprintf("set error\n");
        return ;
    }
    char path[MAX_PATH_LEN+1];
    ret=fm_records_back(path,MAX_PATH_LEN);
    if(ret<0)
    {
        dprintf("back err\n");
        return ;
    }
    dprintf("back1: %s\n",path);
    ret=fm_records_back(path,MAX_PATH_LEN);
    if(ret<0)
    {
        dprintf("back err\n");
        return ;
    }
    dprintf("back2: %s\n",path);
    ret=fm_records_forward(path,MAX_PATH_LEN);
    if(ret<0)
    {
        dprintf("forward err\n");
        return ;
    }
    dprintf("forward1: %s\n",path);
    ret=fm_records_forward(path,MAX_PATH_LEN);
    if(ret<0)
    {
        dprintf("forward err\n");
        //return ;
    }
    dprintf("forward2: %s\n",path);
    ret=fm_records_back(path,MAX_PATH_LEN);
    if(ret<0)
    {
        dprintf("back err\n");
        return ;
    }
    dprintf("back3: %s\n",path);
     ret=fm_records_back(path,MAX_PATH_LEN);
    if(ret<0)
    {
        dprintf("back err\n");
        return ;
    }
    dprintf("back4: %s\n",path);
}
#endif

int fm_records_init()
{
    fm_records.top=0;
    fm_records.bottom=0;
    fm_records.cur=0;
    fm_records.records=0;
    for(int i=0;i<FM_MAX_STACK;i++)
    {
        fm_records.buff[i].path[0]='\0';
        fm_records.buff[i].flags=0;
    }
}

int fm_records_set(char *path)
{
    if(fm_records.top+1<=FM_MAX_STACK)  //check if overflow
    {
        int len=strlen(path);
        strncpy(fm_records.buff[fm_records.top].path,path,min(len,MAX_PATH_LEN));
        fm_records.top++;
        fm_records.records++;
        fm_records.cur=fm_records.top-1;

        dprintf("[fm records] set %s\n",path);

        return 0;
    }
    return -1;
}

int fm_records_back(char *path,int len)
{
    int records=fm_records.records;

    if(records>0)
    {
        int idx=fm_records.cur-1;
        if(idx<fm_records.bottom)
        {
            path[0]='\0';
            return -1;
        }
        int bufflen=strlen(fm_records.buff[idx].path);
        strncpy(path,fm_records.buff[idx].path,min(len,bufflen));
        //update cur pointer
        fm_records.cur--;

        dprintf("[fm records] up %s\n",path);

        return 0;
    }
    return -1;
}

int fm_records_forward(char *path,int len)
{
    int records=fm_records.records;

    if(records>0)
    {
        int idx=fm_records.cur+1;
        if(idx>=fm_records.top)
        {
            path[0]='\0';
            return -1;
        }

        int bufflen=strlen(fm_records.buff[idx].path);
        strncpy(path,fm_records.buff[idx].path,min(len,bufflen));
        //udpate cur pointer 
        fm_records.cur++;

        dprintf("[fm records] down %s\n",path);

        return 0;
    }
    return -1;
}

int fm_records_clear()
{
    fm_records.records=0;
    fm_records.cur=fm_records.top=0;
}